package com.edu.hre.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;



import com.edu.hre.db.config.BaseDAO;
import com.edu.hre.db.config.DBPool;
import com.edu.hre.db.dto.HreEmpBasicsalary;
import com.edu.hre.db.dto.HreEmpEmergencyContact;
import com.edu.hre.db.dto.HreEmpDependent;
import com.edu.hre.db.dto.HreEmpLicense;
import com.edu.hre.db.dto.HreEmpWorkExperience;


public class EmpLicenseDAO  extends BaseDAO {
	private static final Logger logger = Logger.getLogger(EmpLicenseDAO.class);
	@SuppressWarnings("deprecation")
	public List<HreEmpLicense> getLicenseByEmployeeId(
			int employeeNumber) {
		List<HreEmpLicense> listLicense = new ArrayList<HreEmpLicense>();
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		try {
			conn = DBPool.getConnection();
			String strSQLQuery = "SELECT * FROM hre_emp_license el"+
			" JOIN hre_license l ON el.license_id = l.id "+
			" WHERE emp_number = "
					+ employeeNumber;
			preStmt = conn.prepareStatement(strSQLQuery);
			rs = preStmt.executeQuery();
			while (rs.next()) {
				HreEmpLicense license = new HreEmpLicense();
				license.setLicenseNo(rs.getString("license_no"));
				Date d = null;
				if(rs.getDate("license_issued_date") != null){
					d= (rs.getDate("license_issued_date"));
					d.setYear(d.getYear()-1900);
				}
				license.setLicenseIssuedDate(d);
				if(rs.getDate("license_expiry_date") != null){
					d= (rs.getDate("license_expiry_date"));
					d.setYear(d.getYear()-1900);
				}
				license.setLicenseExpiryDate(d);
				license.setLicenseName(rs.getString("name"));
				
				//.....
				//.....
				listLicense.add(license);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// đóng PreparedStatement, ResultSet và đẩy connection vào pool
			releaseConnection(conn, preStmt, rs);
		}
		return listLicense;
	}

}
